package com.tanhua.server.controller;

import com.tanhua.domain.db.User;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.server.interceptors.UserHolder;
import com.tanhua.server.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@RestController
@RequestMapping("/users")
public class UserInfoController {

    @Autowired
    private UserService userService;

//    读取用户资料
//    请求方式：GET
//    请求路径：/users
//    请求参数：请求头Authorization  Query参数：userID  huanxinID
//    返回结果：UserInfoVo
    @GetMapping(name = "读取用户资料")
    public ResponseEntity queryUserInfo(@RequestHeader("Authorization") String token,Long userID,Long huanxinID) throws Exception{
//        这三个参数的优先级：huanxinID    userID   token
        Long id = null;
        if(huanxinID!=null){
            id = huanxinID;
        }else{
            if(userID!=null){
                id = userID;
            }else{
                User user = userService.getUserFromToken(token);
                id = user.getId();
            }
        }
        return userService.findUserInfoById(id);
    }

//    保存用户资料
//    请求方式：PUT
//    请求路径：/users
//    请求参数：请求头Authorization  Body参数：json个数 UserInfo
//    返回结果：UserInfoVo
    @PutMapping(name = "保存用户资料")
    public ResponseEntity updateUserInfo(@RequestBody UserInfo userInfo) throws Exception{
        return userService.updateUserInfo(userInfo);
    }
//    修改用户头像
//    请求方式：POST
//    请求路径：/users/header
//    请求参数：请求头Authorization  文件参数：headPhoto
//    返回结果：无
    @PostMapping(value = "/header",name = "修改用户头像")
    public ResponseEntity updateUserInfoHeader(@RequestHeader("Authorization") String token, MultipartFile headPhoto) throws Exception{
        return userService.loginReginfoHead(token,headPhoto);
    }


    //互相喜欢，喜欢，粉丝 - 统计
    //请求方式：get  users/counts
    //请求参数：token
    //返回：eachLoveCount、loveCount、fanCount
    @GetMapping(value = "/counts",name = "互相喜欢，喜欢，粉丝 - 统计")
    public ResponseEntity eachLove(){
        return userService.eachLove();
    }

    //互相喜欢、喜欢、粉丝 - 翻页列表
    //请求方式：get /users/friends/:type
    //请求参数：token
    //路径参数：type
    //query参数：page、pagesize、nickname
    //返回数据：PageResult
    @GetMapping(value = "/friends/{type}",name = "互相喜欢、喜欢、粉丝、谁看过我 - 翻页列表")
    public ResponseEntity EachLovePage(@PathVariable("type") Integer type,
                                        @RequestParam(defaultValue = "1") Integer page,
                                        @RequestParam(defaultValue = "5") Integer pagesize,
                                        @RequestParam(required = false) String nickname){
        if (type == 1){
            return userService.eachLovePage(page,pagesize,nickname);
        }else if (type == 2){
            return userService.lovePage(page,pagesize,nickname);
        }else if (type == 3){
            return userService.fansPage(page,pagesize,nickname);
        }
        return null;
    }

    //粉丝-喜欢
    //请求方式：post /users/fans/:uid
    //请求参数：token
    //路径参数：uid
    //返回：null
    //uid：当前用户的id
//如果喜欢成为好友记录到Friend中并且记录到环信上
    @PostMapping(value = "/fans/{uid}",name = "粉丝-喜欢")
    public ResponseEntity fansLove(@PathVariable("uid") String uid){
        Long fanId = Long.parseLong(uid);  //TODO 实战
        return userService.fansLove(fanId);
    }

    //喜欢-取消
    //请求方式：delete /users/like/:uid
    //路径参数:uid
    //返回：null
    //uid：喜欢的用户的id
    @DeleteMapping(value = "/like/{uid}",name = "喜欢-取消")
    public ResponseEntity deleteLove(@PathVariable("uid") String uid){
        Long likeUserId = Long.parseLong(uid);  //TODO 实战
        return userService.deleteLove(likeUserId);
    }
}